<?php
namespace App\Traits;

use Workerman\MySQL\Connection;

trait WorkerMysqlConnect
{
    private static $host;

    private static $port;

    private static $username;

    private static $password;

    private static $database;

    public static function connectWorker($ignoreTable = false)
    {
        self::init();
        if($ignoreTable) {
            $table = (new Connection(self::$host, self::$port, self::$username, self::$password, self::$database));
        }else{
            $table = (new Connection(self::$host, self::$port, self::$username, self::$password, self::$database))->from(self::table());
        }
        return $table;
    }

    public static function init()
    {
        self::host($_ENV['DB_HOST']);
        self::port($_ENV['DB_PORT']);
        self::username($_ENV['DB_USERNAME']);
        self::password($_ENV['DB_PASSWORD']);
        self::database($_ENV['DB_DATABASE']);
    }

    protected static function host($host)
    {
        self::$host = $host;
    }

    protected static function port($port)
    {
        self::$port = $port;
    }

    protected static function username($username)
    {
        self::$username = $username;
    }

    protected static function password($password)
    {
        self::$password = $password;
    }

    protected static function database($database)
    {
        self::$database = $database;
    }

    public static function table()
    {
        return (new self())->table;
    }
}
